
#include "Interface.h"
#include "Shader.h"
#include "GLWidget.h"
#include "CreateWorld.h"

#include <iostream>
#include <memory>
#include <thread>

#include <GL/glut.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>

void wsl_init() __attribute__((constructor));
void wsl_init()
{
    char *wslDistro = std::getenv("WSL_DISTRO_NAME");
    if (wslDistro && wslDistro[0] != '\0') {
        setenv("DISPLAY", "172.20.10.3:0.0", 1);
        setenv("MESA_GL_VERSION_OVERRIDE", "3.3", 1);
    }
}

static Interface *interFace = nullptr;
static CreateWorld *createWorld = CreateWorld::getInstance();

GLuint VBO, VAO;

std::shared_ptr<Shader> shaderTest;

// 顶点数据
GLfloat vertices[] = {
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f};

// 初始化所有的数据
void initAll();
// 创建世界地图
void createMap();
// 加载地图
void loadMap();
// 画开始画面文字
void drawBeginText(const char *text);

void display(void)
{
    // 清空颜色缓冲
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    Interface::getInstance()->drawDebug(10, 100);

    // 使用着色器程序
    shaderTest->use();
    glBindVertexArray(VAO);

    // 设置 uniform 变量值
    glm::mat4 modelMatrix = glm::mat4(1.0f); // 初始化为单位矩阵
    glm::mat4 projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f);
    shaderTest->setMat4("modelMatrix", modelMatrix); // 设置模型矩阵值
    shaderTest->setMat4("viewMatrix", modelMatrix); // 设置视图矩阵值
    shaderTest->setMat4("projectionMatrix", projection); // 设置投影矩阵值
    shaderTest->setVec3("color", 1.0f, 0.5f, 0.2f); // 设置颜色值

    // 绘制三角形
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);

    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    // 使用glutInitDisplayMode设置多重采样
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE);

    GLWidget::getInstance();
    initAll();
    loadMap();
    shaderTest = std::make_shared<Shader>("./resource/Shader/test/test.vs", "./resource/Shader/test/test.fs");
    glutDisplayFunc(display);

    // 创建顶点缓冲对象（VBO）
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    // 设置顶点属性指针
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)0);
    glEnableVertexAttribArray(0);

    glutMainLoop();

    glDeleteBuffers(1, &VBO);
    glDeleteVertexArrays(1, &VAO);

    return 0;
}

void initAll()
{
    // worldModel->initSkybox();
    interFace = Interface::getInstance();
    interFace->init();
    // eliminateAnima->init();

    // painter->cube1.init();
    // painter->cube2.init();
    // painter->cube3.init();
    // painter->cube4.init();
    // painter->cube5.init();
    // painter->cube6.init();
    // painter->cube7.init();
    // painter->cube8.init();
    // painter->cube9.init();
    // painter->cube10.init();
    // painter->cube11.init();
    // painter->cube12.init();

    // painter->leafRed.init();
    // painter->leafGreen.init();
    // painter->leafWhite.init();
    // painter->leafYellow.init();

    // painter->person1.init();
    // painter->person2.init();
    // painter->takeCubeInit();

    // painter->plant1.init();
    // painter->plant2.init();
    // painter->plant3.init();
    // painter->plant4.init();
    // painter->plant5.init();
    // painter->plant6.init();

    createWorld->initMapVec();
}

void createMap()
{
    createWorld->CreateCubeMap();
    createWorld->CreatePlantMap();
    createWorld->CreateTreeMap();
}

void loadMap()
{
    drawBeginText("Load Map ...");
    this_thread::sleep_for(3s);

    // player1 = &painter->person1;

    // 创建世界
    createMap();
}

void drawBeginText(const char *text)
{
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    interFace->drawText(text);
    glutSwapBuffers();
    glutPostRedisplay();
}
